package com.maimang.platform.dsp.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.dsp.entity.Fund;
import com.maimang.platform.dsp.service.FundDailyService;
import com.maimang.platform.dsp.service.FundService;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname FundController
 * @description <br>账户余额 信息操作处理</br>
 * @date 2019-07-31 10:07:36
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/dsp/fund")
@Api(value = "账户余额", description = "账户余额 信息操作处理接口")
public class FundController extends BaseController {
	private String prefix = "dsp/fund/data";

	@Autowired
	private FundService fundService;

	@Autowired
	private FundDailyService fundDailyService;

	@RequiresPermissions("dsp:fund:view")
	@GetMapping()
	public String fund() {
		return prefix + "/fund";
	}

	/**
	 * 查询账户余额列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询账户余额列表信息")
	@RequiresPermissions("dsp:fund:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Fund fund) {
		startPage();
		List<Fund> list = fundService.selectFundList(fund);
		return getDataTable(list);
	}

	/**
	 * 查询账户日流水
	 */
	@RequiresPermissions("dsp:fund:list")
	@GetMapping("/daily/{advertiserId}")
	public String detail(@PathVariable("advertiserId") Long advertiserId, ModelMap modelMap) {
		modelMap.put("daily", fundDailyService.selectDailyByAdvertiserId(advertiserId));
		List<Fund> fundList = new ArrayList<>();
		modelMap.put("fundList", fundList);
		return prefix + "/daily/daily";
	}

	/**
	 * 导出账户余额列表
	 */
	@ApiOperation(value = "导出列表", notes = "导出账户余额列表信息到Excel文件")
	@RequiresPermissions("dsp:fund:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(Fund fund) {
		List<Fund> list = fundService.selectFundList(fund);
		ExcelUtil<Fund> util = new ExcelUtil<Fund>(Fund.class);
		return util.exportExcel(list, "fund");
	}

	/**
	 * 新增账户余额
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存账户余额
	 */
	@ApiOperation(value = "新增保存", notes = "参数：Fund")
	@RequiresPermissions("dsp:fund:add")
	@Log(title = "账户余额", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Fund fund) {
		return toResult(fundService.insertFund(fund));
	}

	/**
	 * 修改账户余额
	 */
	@GetMapping("/edit/{advertiserId}")
	public String edit(@PathVariable("advertiserId") Long advertiserId, ModelMap modelMap) {
		Fund fund = fundService.syncFundById(advertiserId);
		modelMap.put("fund", fund);
		return prefix + "/edit";
	}

	/**
	 * 修改保存账户余额
	 */
	@ApiOperation(value = "修改保存", notes = "参数：Fund")
	@RequiresPermissions("dsp:fund:edit")
	@Log(title = "账户余额", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Fund fund) {
		return toResult(fundService.updateFund(fund));
	}

	/**
	 * 删除账户余额
	 */
	@ApiOperation(value = "删除对象", notes = "参数：ids，示例： id1,id2,id3，接口会转转为String[]类型")
	@RequiresPermissions("dsp:fund:remove")
	@Log(title = "账户余额", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(fundService.deleteFundByIds(ids));
	}

}